package server

import (
	"net/http"
	"net/url"
	"sieve_admin_server_userpass/pkg/util/argsRule"
)

type Context struct {
	Uid  int
	Args url.Values
	Http *ContextHttp
}

type ContextHttp struct {
	ResponseWriter http.ResponseWriter
	Request        *http.Request
}

func (c *Context) HasPermission(perm string) bool {
	return checkUserPerm(c.Uid, perm)
}
func (c *Context) HasPermissionsOr(perms ...string) bool {
	for _, perm := range perms {
		if ok := checkUserPerm(c.Uid, perm); ok {
			return true
		}
	}
	return false
}
func (c *Context) HasPermissionsAnd(perms ...string) bool {
	for _, perm := range perms {
		if ok := checkUserPerm(c.Uid, perm); !ok {
			return false
		}
	}
	return true
}
func (c *Context) HasNotAdminPermission(perm string) bool {
	return !isAdmin(c.Uid) && checkUserPerm(c.Uid, perm)
}

func (c *Context) HasNotAdminPermissionsOr(perms ...string) bool {
	for _, perm := range perms {
		if ok := c.HasNotAdminPermission(perm); ok {
			return true
		}
	}
	return false
}

// 参数校验
func (c *Context) VerifyArgs(required ...argsRule.RuleInfo) error {
	return argsRule.VerifyArgs(c.Args, required...)
}
